<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统配置 - Qwen2-VL Demo</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 800px;
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 20px;
            text-align: center;
            position: relative;
        }

        .header h1 {
            font-size: 24px;
            margin-bottom: 5px;
        }

        .header p {
            opacity: 0.9;
            font-size: 14px;
        }

        .back-button {
            position: absolute;
            left: 20px;
            top: 50%;
            transform: translateY(-50%);
            background: rgba(255,255,255,0.2);
            color: white;
            border: none;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
            text-decoration: none;
            transition: background-color 0.3s;
        }

        .back-button:hover {
            background: rgba(255,255,255,0.3);
        }

        .config-content {
            padding: 30px;
        }

        .config-section {
            margin-bottom: 30px;
        }

        .config-section h3 {
            color: #333;
            margin-bottom: 15px;
            font-size: 18px;
            border-bottom: 2px solid #f0f0f0;
            padding-bottom: 8px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #555;
        }

        .form-group input,
        .form-group textarea,
        .form-group select {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s;
            resize: vertical;
        }

        .form-group input:focus,
        .form-group textarea:focus,
        .form-group select:focus {
            outline: none;
            border-color: #007bff;
        }

        .form-group textarea {
            min-height: 120px;
            font-family: inherit;
            line-height: 1.5;
        }

        .form-group .help-text {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }

        .button-group {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 30px;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }

        .btn-primary {
            background: #007bff;
            color: white;
        }

        .btn-primary:hover {
            background: #0056b3;
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
        }

        .btn-success {
            background: #28a745;
            color: white;
        }

        .btn-success:hover {
            background: #218838;
        }

        .alert {
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
        }

        .alert.show {
            display: block;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .preset-buttons {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 15px;
        }

        .preset-btn {
            padding: 6px 12px;
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 6px;
            font-size: 12px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .preset-btn:hover {
            background: #e9ecef;
        }

        .preset-btn.active {
            background: #007bff;
            color: white;
            border-color: #007bff;
        }

        @media (max-width: 768px) {
            .container {
                border-radius: 0;
                min-height: 100vh;
            }
            
            .config-content {
                padding: 20px;
            }
            
            .button-group {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <a href="/" class="back-button">←</a>
            <h1>⚙️ 系统配置</h1>
            <p>配置模型参数和系统提示词</p>
        </div>
        
        <div class="config-content">
            <div class="alert alert-success" id="successAlert">
                配置已保存成功！
            </div>
            
            <div class="alert alert-error" id="errorAlert">
                保存配置失败，请重试。
            </div>
            
            <form id="configForm">
                <div class="config-section">
                    <h3>🤖 模型配置</h3>
                    
                    <div class="form-group">
                        <label for="temperature">Temperature (创造性)</label>
                        <input type="range" id="temperature" name="temperature" min="0" max="2" step="0.1" value="0.7">
                        <div class="help-text">控制回答的随机性，0-2之间，越高越有创造性</div>
                    </div>
                    
                    <div class="form-group">
                        <label for="maxTokens">Max Tokens (最大长度)</label>
                        <input type="number" id="maxTokens" name="maxTokens" min="100" max="4000" value="2000">
                        <div class="help-text">控制回答的最大长度，100-4000之间</div>
                    </div>
                </div>
                
                <div class="config-section">
                    <h3>💬 系统提示词</h3>
                    
                    <div class="form-group">
                        <label for="systemPrompt">System Prompt</label>
                        <div class="preset-buttons">
                            <button type="button" class="preset-btn" data-preset="default">默认</button>
                            <button type="button" class="preset-btn" data-preset="helpful">助手</button>
                            <button type="button" class="preset-btn" data-preset="creative">创意</button>
                            <button type="button" class="preset-btn" data-preset="professional">专业</button>
                            <button type="button" class="preset-btn" data-preset="polyhedron">多面体检测</button>
                            <button type="button" class="preset-btn" data-preset="custom">自定义</button>
                        </div>
                        <textarea id="systemPrompt" name="systemPrompt" placeholder="输入系统提示词..."></textarea>
                        <div class="help-text">系统提示词将影响AI的回答风格和行为</div>
                    </div>
                </div>
                
                <div class="config-section">
                    <h3>🎨 界面设置</h3>
                    
                    <div class="form-group">
                        <label for="chatTitle">聊天标题</label>
                        <input type="text" id="chatTitle" name="chatTitle" value="🤖 Qwen2-VL 对话Demo">
                        <div class="help-text">自定义聊天界面的标题</div>
                    </div>
                    
                    <div class="form-group">
                        <label for="welcomeMessage">欢迎消息</label>
                        <textarea id="welcomeMessage" name="welcomeMessage" rows="3" placeholder="输入欢迎消息..."></textarea>
                        <div class="help-text">用户首次访问时显示的欢迎消息</div>
                    </div>
                </div>
                
                <div class="button-group">
                    <button type="button" class="btn btn-secondary" onclick="resetConfig()">重置</button>
                    <button type="button" class="btn btn-success" onclick="saveConfig()">保存配置</button>
                    <a href="/" class="btn btn-primary">返回聊天</a>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 预设提示词（将从API动态加载）
        let presets = {
            default: "你是一个有用的AI助手，可以回答问题和协助用户完成各种任务。",
            helpful: "你是一个友善、有帮助的AI助手。请用温和、耐心的语气回答用户的问题，并尽可能提供详细和有用的信息。",
            creative: "你是一个富有创造力的AI助手。请用生动、有趣的方式回答问题，可以使用比喻、故事或创新的角度来解释问题。",
            professional: "你是一个专业的AI顾问。请用准确、简洁、专业的语言回答问题，提供基于事实的信息和建议。",
            polyhedron: "多面体检测模式",
            custom: ""
        };

        // 从API加载预设提示词
        async function loadPresets() {
            try {
                const response = await fetch('/api/prompts');
                const data = await response.json();
                if (data.success) {
                    presets = { ...presets, ...data.prompts };
                }
            } catch (error) {
                console.error('加载预设提示词失败:', error);
            }
        }

        // 默认配置
        const defaultConfig = {
            temperature: 0.7,
            maxTokens: 2000,
            systemPrompt: presets.default,
            chatTitle: "🤖 Qwen2-VL 对话Demo",
            welcomeMessage: "您好！我是Qwen2-VL，可以进行文本对话和图片识别。请发送消息或上传图片开始对话！"
        };

        // 加载配置
        function loadConfig() {
            const config = JSON.parse(localStorage.getItem('qwen2vl_config') || JSON.stringify(defaultConfig));
            
            document.getElementById('temperature').value = config.temperature;
            document.getElementById('maxTokens').value = config.maxTokens;
            document.getElementById('systemPrompt').value = config.systemPrompt;
            document.getElementById('chatTitle').value = config.chatTitle;
            document.getElementById('welcomeMessage').value = config.welcomeMessage;
            
            updateTemperatureLabel();
        }

        // 保存配置
        function saveConfig() {
            const config = {
                temperature: parseFloat(document.getElementById('temperature').value),
                maxTokens: parseInt(document.getElementById('maxTokens').value),
                systemPrompt: document.getElementById('systemPrompt').value,
                chatTitle: document.getElementById('chatTitle').value,
                welcomeMessage: document.getElementById('welcomeMessage').value
            };
            
            try {
                localStorage.setItem('qwen2vl_config', JSON.stringify(config));
                showAlert('success', '配置已保存成功！');
            } catch (error) {
                showAlert('error', '保存配置失败：' + error.message);
            }
        }

        // 重置配置
        function resetConfig() {
            if (confirm('确定要重置所有配置吗？')) {
                localStorage.removeItem('qwen2vl_config');
                loadConfig();
                showAlert('success', '配置已重置！');
            }
        }

        // 显示提示
        function showAlert(type, message) {
            const alertId = type === 'success' ? 'successAlert' : 'errorAlert';
            const alertElement = document.getElementById(alertId);
            
            // 隐藏其他提示
            document.querySelectorAll('.alert').forEach(el => el.classList.remove('show'));
            
            // 显示当前提示
            alertElement.textContent = message;
            alertElement.classList.add('show');
            
            // 3秒后自动隐藏
            setTimeout(() => {
                alertElement.classList.remove('show');
            }, 3000);
        }

        // 更新温度标签
        function updateTemperatureLabel() {
            const temp = document.getElementById('temperature');
            const label = temp.previousElementSibling;
            label.textContent = `Temperature (创造性): ${temp.value}`;
        }

        // 预设按钮点击
        function handlePresetClick(preset) {
            const textarea = document.getElementById('systemPrompt');
            textarea.value = presets[preset];
            
            // 更新按钮状态
            document.querySelectorAll('.preset-btn').forEach(btn => {
                btn.classList.remove('active');
            });
            document.querySelector(`[data-preset="${preset}"]`).classList.add('active');
        }

        // 初始化
        document.addEventListener('DOMContentLoaded', async function() {
            await loadPresets();
            loadConfig();
            
            // 温度滑块事件
            document.getElementById('temperature').addEventListener('input', updateTemperatureLabel);
            
            // 预设按钮事件
            document.querySelectorAll('.preset-btn').forEach(btn => {
                btn.addEventListener('click', function() {
                    const preset = this.dataset.preset;
                    handlePresetClick(preset);
                });
            });
            
            // 检测当前使用的预设
            const currentPrompt = document.getElementById('systemPrompt').value;
            for (const [key, value] of Object.entries(presets)) {
                if (value === currentPrompt) {
                    document.querySelector(`[data-preset="${key}"]`).classList.add('active');
                    break;
                }
            }
        });
    </script>
</body>
</html> 